unit con_TitNF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Formulario_Padrao_Consulta, TFlatButtonUnit, StdCtrls, Buttons,
  ExtCtrls, DB, DBClient, Grids, DBGrids, RXCtrls,dbtables;

type
  TfrmCon_TitNF = class(TFrmConsulta)
    RgTipo: TRadioGroup;
    dbgTitCredito: TDBGrid;
    CDS: TClientDataSet;
    CDStitc_numero: TStringField;
    CDStipd_codigo: TStringField;
    CDSemp_codigo: TStringField;
    CDStitc_dataemis: TDateField;
    CDStitc_datavenc: TDateField;
    CDStitc_valor: TFloatField;
    CDStitc_saldo: TFloatField;
    CDStitc_datapgto: TDateField;
    CDStitc_valorpago: TFloatField;
    CDSbord_numero: TStringField;
    CDSLocal_codigo: TStringField;
    CDStitc_obs: TBlobField;
    CDStitc_nossonum: TStringField;
    LblSaldo: TRxLabel;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dbgTitCreditoTitleClick(Column: TColumn);
    procedure RgTipoClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure dbgTitCreditoDblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    xNota,
    xSerie,
    xEntidade: String;
    pTpForm  : Byte;
    procedure FazConsulta;
  end;

var
  frmCon_TitNF: TfrmCon_TitNF;


implementation

uses clsBackupVenda, Cad_TitCredito, Biblio, Dm, Rel_Empenho,
  clsBackup_NotaCompra;

var
 BackupVenda: TBackupVenda;
 BackupCompra:TBackup_NotaCompra;

{$R *.dfm}

{ TfrmCon_TitNF }

procedure TfrmCon_TitNF.FazConsulta;
var
 tq: TQuery;
 xSql: String;
 nota1,nota2: String;
 saldo: Double;
begin
 saldo:= 0;
 dbgTitcredito.Columns[6].Visible := false;
 dbgTitcredito.Columns[7].Visible := false;
 xSql:= '';
 if (_Tipocad = 'R') or (_Tipocad = '') then begin
  BackupVenda.RecuperarNota(xSerie,xNota,nota1,nota2);
  if rgtipo.itemindex=0 then
   xsql:= 'Select * from TitCredito where Nota_Serie ='+QuotedStr(xSerie)+' and '+
          '(nota_numero = '+QuotedStr(nota1)+' or nota_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade);
  if rgtipo.itemindex=1 then
   xsql:= 'Select * from TitCredito where Nota_Serie ='+QuotedStr(xSerie)+' and '+
          '(nota_numero = '+QuotedStr(nota1)+' or nota_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade)+' and titc_saldo <> 0 ';
  if rgtipo.itemindex=2 then begin
   dbgTitcredito.Columns[6].Visible := True;
   dbgTitcredito.Columns[7].Visible := True;
   xsql:= 'Select * from TitCredito where Nota_Serie ='+QuotedStr(xSerie)+' and '+
          '(nota_numero = '+QuotedStr(nota1)+' or nota_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade)+' and titc_saldo = 0 ';
  end;
 end else begin
  BackupCompra.Retorna_Notas(xNota,xSerie,xEntidade,nota1,nota2,xSerie);
  if rgtipo.itemindex=0 then
   xsql:= 'Select * from TitCredito where Notc_Serie ='+QuotedStr(xSerie)+' and '+
          '(notac_numero = '+QuotedStr(nota1)+' or notac_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade);
  if rgtipo.itemindex=1 then
   xsql:= 'Select * from TitCredito where Notc_Serie ='+QuotedStr(xSerie)+' and '+
          '(notac_numero = '+QuotedStr(nota1)+' or notac_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade)+' and titc_saldo <> 0 ';
  if rgtipo.itemindex=2 then begin
   dbgTitcredito.Columns[6].Visible := True;
   dbgTitcredito.Columns[7].Visible := True;
   xsql:= 'Select * from TitCredito where Notc_Serie ='+QuotedStr(xSerie)+' and '+
          '(notac_numero = '+QuotedStr(nota1)+' or notac_numero = '+QuotedStr(nota2)+') and '+
          'ent_codigo = '+ QuotedStr(xEntidade)+' and titc_saldo = 0 ';
  end;
 end;
 tq:= TQuery.Create(self);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Close;
 tq.SQL.Clear;
 tq.Params.Clear;
 tq.SQL.Add(xsql);
 tq.open;
 CDS.Close;
 CDS.CreateDataSet;
 CDS.Open;
 while not tq.eof do begin
  CDS.Append;
  CDStitc_numero.Value   := tq.FieldByname('titc_numero').AsString;
  CDStipd_codigo.Value   := tq.FieldByname('tipd_codigo').AsString;
  CDSemp_codigo.Value    := tq.FieldByname('emp_codigo').AsString;
  CDStitc_dataemis.Value := tq.FieldByname('titc_dataemis').AsDateTime;
  CDStitc_datavenc.Value := tq.FieldByname('titc_datavenc').AsDateTime;
  CDStitc_valor.Value    := tq.FieldByname('titc_valor').AsFloat;
  CDStitc_saldo.Value    := tq.FieldByname('titc_saldo').AsFloat;
  CDStitc_datapgto.Value := tq.FieldByname('titc_datapgto').AsDateTime;
  CDStitc_valorpago.Value:= tq.FieldByname('titc_valor').AsFloat-tq.FieldByname('titc_saldo').AsFloat;
  CDSbord_numero.Value   := tq.FieldByname('bord_numero').AsString;
  CDSLocal_codigo.Value  := tq.FieldByname('Local_codigo').AsString;
  CDStitc_obs.Value      := tq.FieldByname('titc_obs').AsString;
  saldo := saldo + tq.fieldbyname('titc_saldo').AsFloat;
  tq.next;
 end;
 LblSaldo.Caption := 'Saldo.: '+mascara_valor(saldo);
 CDS.First;
end;

procedure TfrmCon_TitNF.FormCreate(Sender: TObject);
begin
  inherited;
 BackupVenda:= TBackupVenda.Create;
 BackupCompra:= TBackup_NotaCompra.Create;
end;

procedure TfrmCon_TitNF.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
 BackupVenda.Free;
 BackupCompra.Free;
end;

procedure TfrmCon_TitNF.dbgTitCreditoTitleClick(Column: TColumn);
begin
  inherited;
 CDS.IndexFieldNames:= Column.FieldName;
 CDS.First;
end;

procedure TfrmCon_TitNF.RgTipoClick(Sender: TObject);
begin
  inherited;
 FazConsulta;
end;

procedure TfrmCon_TitNF.FormActivate(Sender: TObject);
begin
  inherited;
 FazConsulta;
end;

procedure TfrmCon_TitNF.dbgTitCreditoDblClick(Sender: TObject);
var
 f : TfrmCad_TitCredito;
 f1: TFrmRel_Empenho;
begin
  inherited;
 _Tipocad := 'R';
 if pTpForm = 0 then begin
  f:= TfrmCad_TitCredito.Create(Self);
  f.limpaTela:= True;
  f.edttipd_codigo.Text:= CDStipd_codigo.AsString;
  f.edtEnt_Codigo.Text := xEntidade;
  f.edttitc_numero.Text:= CDStitc_numero.AsString;
  f.limpaTela:= False;
  _consulta:='..';
  f.ShowModal;
  f.Free;
  FazConsulta;
 end else begin
  f1:= TFrmRel_Empenho.Create(self);
  f1.edtEnt_Codigo.text:= xEntidade;
  f1.xNumTitEmpenho    := CDStitc_numero.AsString;
  f1.edtdatavenc.date  := CDStitc_datavenc.AsDateTime;
  f1.ShowModal;
  f1.Free;
 end;
end;

end.
